package serv

import (
	"context"
	"generate_grom_mode_and_dao/service/genertategrom/api/internal/svc"
	"generate_grom_mode_and_dao/service/genertategrom/api/model"
	"github.com/tal-tech/go-zero/core/logx"
	"strings"
)

type TableFieldService struct {
}

type defaultTableFieldService struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewTableFieldService(ctx context.Context, svcCtx *svc.ServiceContext) *defaultTableFieldService {
	return &defaultTableFieldService{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (s defaultTableFieldService) GetFieldJson(field model.TableFields) string {
	return `json:"` + field.Field + `" ` + `gorm:"column:` + field.Field + `"`
}

// 获取字段说明
func (s defaultTableFieldService) GetFieldComment(field model.TableFields) string {
	if len(field.Comment) > 0 {
		return "// " + field.Comment
	}
	return ""
}

// 获取字段类型
func (s defaultTableFieldService) GetFiledType(field model.TableFields) string {
	typeArr := strings.Split(field.Type, "(")
	switch typeArr[0] {
	case "int":
		return "int"
	case "integer":
		return "int"
	case "mediumint":
		return "int"
	case "bit":
		return "int"
	case "year":
		return "int"
	case "smallint":
		return "int"
	case "tinyint":
		return "int"
	case "bigint":
		return "int64"
	case "decimal":
		return "float32"
	case "double":
		return "float32"
	case "float":
		return "float32"
	case "real":
		return "float32"
	case "numeric":
		return "float32"
	case "timestamp":
		return "time.Time"
	case "datetime":
		return "time.Time"
	case "time":
		return "time.Time"
	default:
		return "string"
	}
}

// 获取字段类型
func (s defaultTableFieldService) GetFiledTypeCommon(field string) string {
	switch field {
	case "int":
		return "int"
	case "integer":
		return "int"
	case "mediumint":
		return "int"
	case "bit":
		return "int"
	case "year":
		return "int"
	case "smallint":
		return "int"
	case "tinyint":
		return "int"
	case "bigint":
		return "int64"
	case "decimal":
		return "float32"
	case "double":
		return "float32"
	case "float":
		return "float32"
	case "real":
		return "float32"
	case "numeric":
		return "float32"
	case "timestamp":
		return "time.Time"
	case "datetime":
		return "time.Time"
	case "time":
		return "time.Time"
	default:
		return "string"
	}
}
